package com.yyou.chat_gpt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yyou.chat_gpt.entity.ChatHistoryInfo;
import com.yyou.chat_gpt.entity.UserInfo;
import com.yyou.chat_gpt.mapper.IChatHistiryInfoMapper;
import com.yyou.chat_gpt.service.ChatHistoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Wrapper;
import java.util.List;

/**
 * @ClassName ChatHistoryServiceImpl
 * @Author ZengRJ
 * @Date 2023/7/30 14:34
 * @Version 1.0
 */
@Slf4j
@Service
public class ChatHistoryServiceImpl implements ChatHistoryService {

    @Resource
    private IChatHistiryInfoMapper chatHistiryInfoMapper;

    /**
     * 保存聊天历史记录
     * @param chatHistoryInfo
     * @return
     */
    @Override
    public int insertChatHistory(ChatHistoryInfo chatHistoryInfo) {
        return chatHistiryInfoMapper.insert(chatHistoryInfo);
    }

    /**
     * 获取用户聊天历史记录列表
     * @param userInfo
     * @return
     */
    @Override
    public List<ChatHistoryInfo> getChatHistoryList(UserInfo userInfo) {
        QueryWrapper<ChatHistoryInfo> queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", userInfo.getId()).eq("type", "msg").eq("valid_status", "1");
        return chatHistiryInfoMapper.selectList(queryWrapper);
    }

    /**
     * 清除聊天历史记录
     * @param userInfo
     * @return
     */
    @Override
    public int clearChatHistory(UserInfo userInfo) {
        ChatHistoryInfo chatHistoryInfo = new ChatHistoryInfo();
        chatHistoryInfo.setValidStatus(0);
        QueryWrapper<ChatHistoryInfo> queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", userInfo.getId()).eq("type", "msg").eq("valid_status", "1");
        return chatHistiryInfoMapper.update(chatHistoryInfo, queryWrapper);
    }
}
